package com.shenhaofeng.app.camera;

import android.content.Context;
import android.net.Uri;

import java.io.File;

/**
 * Created by Administrator on 2016/3/23.
 */
public class Option {

    private static final String DEFAULT_CACHE_DIR_NAME = "camera";


    private Uri outputUri;


    private Option() {
    }


    /**
     * 构建默认的输出文件uri
     *
     * @param context
     * @param name
     * @return
     */
    public static final Uri getDefaultUri(Context context, String name) {
        File dir = new File(context.getExternalCacheDir(), DEFAULT_CACHE_DIR_NAME);
        boolean dirCreated;
        if (!dir.exists()) {
            dirCreated = dir.mkdirs();
        } else {
            dirCreated = true;
        }
        if (!dirCreated) {
            dir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR_NAME);
            if (!dir.exists()) {
                dirCreated = dir.mkdirs();
            } else {
                dirCreated = true;
            }
        }
        if (!dirCreated) {
            throw new RuntimeException("there is no storage");
        }
        return Uri.fromFile(new File(dir, name));
    }


    public static final File getDefaultDir(Context context) {
        File dir = new File(context.getExternalCacheDir(), DEFAULT_CACHE_DIR_NAME);
        if (dir.exists()) {
            return dir;
        }
        dir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR_NAME);
        if (dir.exists()) {
            return dir;
        }
        return null;
    }


    public Uri getOutputUri() {
        return outputUri;
    }


    public static class Builder {

        private Uri outputUri;


        /**
         * 配置文件缓存路径Uri
         *
         * @param outputUri
         * @return
         */
        public Builder outputUri(Uri outputUri) {
            this.outputUri = outputUri;
            return this;
        }


        /**
         * 构建Option对象
         *
         * @return
         */
        public Option build() {
            Option option = new Option();
            if (outputUri == null) {
                throw new UnsupportedOperationException("must set outputUri");
            }
            option.outputUri = outputUri;
            return option;
        }


    }


}
